package com.germinus.telcoblocks.echarts.editor.editors;

import org.eclipse.jface.text.rules.*;
import org.eclipse.jface.text.*;
import org.eclipse.swt.SWT;

public class ECHScanner extends RuleBasedScanner {
	private static String[] keywords = { "public", "initial", "state", "transition", "import", "machine",
			"nonterminal", "package", "-->"};

	public ECHScanner(ColorManager manager) {
		IToken javaCode =
			new Token(
				new TextAttribute(
					manager.getColor(IECHColorConstants.JAVA_CODE)));
        IToken tKeyword = new Token(new TextAttribute(manager.getColor(IECHColorConstants.KEYWORD), null, SWT.BOLD));
        IToken singleLineComment = new Token(new TextAttribute(manager.getColor(IECHColorConstants.ECH_SINGLE_LINE_COMMENT)));
		IRule[] rules = new IRule[4];
		rules[0] = new MultiLineRule("<*", "*>", javaCode);
		rules[1] = new WhitespaceRule(new ECHWhitespaceDetector());
		WordRule wordRule = new WordRule(new ECHWordDetector());
		for (String keyword : keywords) {
			wordRule.addWord(keyword, tKeyword);
			
		}
		rules[2] = wordRule;
		rules[3] = new SingleLineRule("//",null,singleLineComment);
		setRules(rules);
	}
	
	public class ECHWordDetector implements IWordDetector {
		public boolean isWordPart(char character) {
			return ((Character.isJavaIdentifierPart(character) || character == '-') || character == '>');
		}
		public boolean isWordStart(char character) {
			return ((Character.isJavaIdentifierStart(character) || character == '-') || character == '>');
		}
		}
	}
